#include "Frm02.h"
#include "util/FileUtil.h"
#include "util/StringUtil.h"
#if WIN32
#include "io.h"
#endif

Frm02::Frm02(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm02::~Frm02() {
}

void Frm02::onInit() {

}

void Frm02::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(1, 1, 98, 48, false);
	wMButton1->setPressedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Dropdown : wMDropdown1               -----------------------------*/
	wMDropdown1 = new STTDropdown(p_rt, this, "wMDropdown1");
	wMDropdown1->setMainArg(105, 8, 812, 32, "选项 1\n选项 2\n选项 3", 0, ScrollDir::BOTTOM, NULL, -1, false);
	wMDropdown1->setSymbol("↓", false);
	wMDropdown1->setFonts(0x0, 16, "文泉驿微米黑", false, 0xffffff, 16, "文泉驿微米黑", 0, 0);
	wMDropdown1->setMainStyle(8, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 0, 0x0);
	wMDropdown1->getStyle()->p_pad->setTop(5)->setBottom(5)->setLeft(11)->setRight(11);
	wMDropdown1->setListSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 0, 0x0);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(925, 1, 98, 48, false);
	wMButton2->setPressedArg("打开", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("打开", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton2_clk_cb);

	/*----------------------------- Panel : wMPanel1                     -----------------------------*/
	wMPanel1 = new STTPanel(p_rt, this, "wMPanel1");
	wMPanel1->setMainArg(0, 100, 1024, 500, 0);
	wMPanel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 255, 1, 0xc0c0c0);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(261, 49, 62, 31, false);
	wMButton3->setPressedArg("Button", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton3->setReleasedArg("首页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton3->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton3_clk_cb);

	/*----------------------------- Button : wMButton4                   -----------------------------*/
	wMButton4 = new STTButton(p_rt, this, "wMButton4", 0);
	wMButton4->setMainArg(322, 49, 62, 31, false);
	wMButton4->setPressedArg("Button", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton4->setReleasedArg("上一页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton4->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton4->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton4_clk_cb);

	/*----------------------------- Button : wMButton5                   -----------------------------*/
	wMButton5 = new STTButton(p_rt, this, "wMButton5", 0);
	wMButton5->setMainArg(383, 49, 62, 31, false);
	wMButton5->setPressedArg("Button", 0x0, "文泉驿微米黑", 16, 0);
	wMButton5->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton5->setReleasedArg("下一页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton5->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton5->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton5_clk_cb);

	/*----------------------------- Button : wMButton6                   -----------------------------*/
	wMButton6 = new STTButton(p_rt, this, "wMButton6", 0);
	wMButton6->setMainArg(444, 49, 62, 31, false);
	wMButton6->setPressedArg("Button", 0x0, "文泉驿微米黑", 16, 0);
	wMButton6->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton6->setReleasedArg("末页", 0x0, "文泉驿微米黑", 12, 0);
	wMButton6->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton6->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton6_clk_cb);

	/*----------------------------- Button : wMButton7                   -----------------------------*/
	wMButton7 = new STTButton(p_rt, this, "wMButton7", 0);
	wMButton7->setMainArg(505, 49, 62, 31, false);
	wMButton7->setPressedArg("Button", 0x0, "文泉驿微米黑", 16, 0);
	wMButton7->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton7->setReleasedArg("放大", 0x0, "文泉驿微米黑", 12, 0);
	wMButton7->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton7->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton7_clk_cb);

	/*----------------------------- Button : wMButton8                   -----------------------------*/
	wMButton8 = new STTButton(p_rt, this, "wMButton8", 0);
	wMButton8->setMainArg(566, 49, 62, 31, false);
	wMButton8->setPressedArg("缩小", 0x0, "文泉驿微米黑", 12, 0);
	wMButton8->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton8->setReleasedArg("缩小", 0x0, "文泉驿微米黑", 12, 0);
	wMButton8->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton8->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton8_clk_cb);

	/*----------------------------- Button : wMButton9                   -----------------------------*/
	wMButton9 = new STTButton(p_rt, this, "wMButton9", 0);
	wMButton9->setMainArg(627, 49, 62, 31, false);
	wMButton9->setPressedArg("旋转", 0x0, "文泉驿微米黑", 12, 0);
	wMButton9->setPressedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton9->setReleasedArg("旋转", 0x0, "文泉驿微米黑", 12, 0);
	wMButton9->setReleasedStyle(50, 255, 0xffffff, 0xffffff, 0, 2, 0x1a2b1, 0, 0x0);
	wMButton9->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton9_clk_cb);

	/*called after widget created.*/

	return; //createParts END
}

vector<string> getPdfFiles() {
	vector<string> paths;
	//1.文件搜索目录s
#if __arm__
	paths.push_back("/customer");//搜索客户目录
	paths.push_back("/vendor");//搜索外设目录
#else
	paths.push_back("./res");//搜索资源目录
#endif
	//2.这些目录下的所有文件清单
	vector<KV_Str_Str> ret;
	for(string path: paths) {
		FileUtil::getPathContent(path,ret);//搜索，注意文件太多可能会慢
	}
	//3.指定的文件类型
	vector<string> result;
	for(KV_Str_Str kv: ret) {
		string filepath = kv.v + "/" + kv.k;
		if(StringUtil::endWithNoCase(filepath, ".pdf")) {
			result.push_back(filepath);
		}
	}
	return result;
}

static void prepareXPDF() {
#if __arm__
	string result;
	Util::exeSync("tar -xzf /customer/prm/res/pic/pdf.tar.gz -C /customer/prm/res/pic", "r", result);
	Util::exeSync("rm /customer/prm/res/pic/pdf.tar.gz", "r", result);//解压后删掉，避免下次打开又解压
	Util::exeSync("chmod +x /customer/prm/res/pic/pdftopng", "r", result);
	Util::exeSync("chmod +x /customer/prm/res/pic/pdfinfo", "r", result);
	Util::exeSync("chmod +x /customer/prm/res/pic/pdffonts", "r", result);
	Util::exeSync("mv /customer/prm/res/res/libpng16.so.16.40.0 /customer/xttlib/libpng16.so.16", "r", result);
	Util::exeSync("sync", "r", result);
#endif
}

static PDFUtil *pdf = NULL;

void Frm02::onLoad() {
	prepareXPDF();
	vector<string> paths = getPdfFiles();
	for(string s: paths)
		wMDropdown1->addOption(s, s);

}

bool Frm02::onClosing() {
	return true;
}

void Frm02::onDispose() {

}

void Frm02::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（打开）的点击事件*/
	if(pdf) {
		delete pdf;
	}
	/**
	 * 解压xpdf文件(见QQ群)
	 * 参数1：解压后的文件夹。在这个文件夹下能找到pdftopng.exe等
	 * 参数2: PDF文件路径
	 * 参数3: 转换后png文件路径及文件前缀。示例中png将保存在d:/pdftool/xpdf文件夹下，生成的png文件名为stt-xxxxxx.png(xxxxxx为6位数字)
	 * 参数4: 面板控件
	 */
	string prefix = "test" + to_string(wMDropdown1->getSelectedIndex());//转成图片文件的前缀，不同文件要各不相同
#if __arm__
	string arg1 = "/customer/prm/res/pic";
	string arg2 = wMDropdown1->getSelectedText();
	string arg3 = arg1 + "/" + prefix;//保存到u盘 string arg3 = "/vendor/udisk_sda1" + "/" + prefix;
	pdf = new PDFUtil("/mnt/xpdf/bin","/mnt/xpdf/bin/V2.1.pdf","/mnt/xpdf/bin/stt",wMPanel1);
#else
	string cwd = getcwd(NULL, 0);
	cwd = StringUtil::replace(cwd, "\\", "/");
	string arg1 = cwd + "/Debug";
	string arg2 = cwd + wMDropdown1->getSelectedText().substr(1);
	string arg3 = arg1 + "/" + prefix;

#endif
	pdf = new PDFUtil(arg1, arg2, arg3, wMPanel1);
	pdf->first();//显示第1页
}

void Frm02::wMButton4_clk_cb(uint16_t code, LvEvent e) {
	/*上一页的点击事件*/
	pdf->back();
}
void Frm02::wMButton9_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton14的点击事件*/
	pdf->rotate();
}
void Frm02::wMButton5_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton10的点击事件*/
	pdf->next();
}
void Frm02::wMButton8_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton13的点击事件*/
	pdf->zoom(0.8);
}
void Frm02::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton8的点击事件*/
	pdf->first();
}
void Frm02::wMButton6_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton11的点击事件*/
	pdf->last();
}
void Frm02::wMButton7_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton12的点击事件*/
	pdf->zoom(1.25);
}


